package com.aboo.action;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;

import com.aboo.po.FrameConstantPO;
import com.aboo.po.MenuPO;
import com.aboo.po.Option;
import com.aboo.pub.exception.BasicException;
import com.aboo.service.FrameConstantService;
import com.aboo.service.MenuService;
import com.aboo.util.AjaxUtil;
import com.aboo.util.RequestUtil;

public class MenuAction extends BaseAction {

	private static final long serialVersionUID = 1174779053681281777L;
	private MenuService menuService;
	private FrameConstantService frameConstantService;
	private MenuPO menu = new MenuPO();

	public String list() throws BasicException {
		return BaseAction.LIST;
	}
	
	public String createSub() throws BasicException {
		String menuParentId = RequestUtil.getParameterStringDefault(request, "menuParentId", "");
		menu.setId(new Integer(menuParentId));
		menuService.findMenu(ri, menu);
		menu = (MenuPO) ri.getObj();
		
		if (Integer.parseInt(menu.getMenuType()) >= 3) {
			throw new BasicException("本菜单不能创建子菜单！");
		}
		return "createSub";
	}
	
	public String createSubExc() throws BasicException {
		menuService.saveMenu(ri, menu);
		return "createSubExc";
	}
	
	public String delete() throws BasicException {
		Integer menuId = menu.getId();
		if (menuId.intValue() == 0) {
			throw new BasicException("系统总菜单，无法删除！");
		}
		
		menu = new MenuPO();
		menu.setParentId(menuId);
		menuService.findMenuCount(ri, menu);
		Integer count = (Integer)ri.getObj();		
		if (count != null && count.intValue() > 0) {
			throw new BasicException("存在下级节点，无法删除！");
		}
		
		List<String> menuIdList = new ArrayList<String>();
		menuIdList.add(menuId.toString());
		menuService.deleteMenu(ri, menuIdList);
		
		return BaseAction.DELETE;
	}
	
	public String modify() throws BasicException {
		String menuId = RequestUtil.getParameterStringDefault(request, "menuId", "");
		menu.setId(new Integer(menuId));
		menuService.findMenu(ri, menu);
		menu = (MenuPO) ri.getObj();
		
		MenuPO parentMenu = new MenuPO();
		if (menu.getParentId() != null && menu.getParentId().intValue() != 0) {
			parentMenu.setId(menu.getParentId());
			menuService.findMenu(ri, parentMenu);
			parentMenu = (MenuPO) ri.getObj();
		}
		
		RequestUtil.setAttribute(request, "parentMenu", parentMenu);
		return BaseAction.MODIFY;
	}

	public String modifyExc() throws BasicException {
		menuService.updateMenu(ri, menu);
		return BaseAction.MODIFY_EXC;
	}
	
	@SuppressWarnings("unchecked")
	public String getAllMenu() throws BasicException {
		MenuPO param = new MenuPO();
		menuService.findMenuList(ri, param, null);
		List<MenuPO> result = (List<MenuPO>) ri.getObj();
		
		String jsonStr = new String("");
		if (result != null) {
			List<Option> ops = new ArrayList<Option>();
			
			FrameConstantPO constant = new FrameConstantPO();
			constant.setModuleName("MENU_TYPE");
			frameConstantService.findFrameConstantList(ri, constant, null);
			List<FrameConstantPO> constantList = (List<FrameConstantPO>)ri.getObj();
			
			//真正的资源部分
			for (MenuPO res : result) {
				ops.add(new Option(res.getId().toString(), res.getName(), res.getParentId().toString()));
			}
			JSONArray ja = JSONArray.fromObject(ops);
			jsonStr = ja.toString();
		}
		
		AjaxUtil.doAjax(response, jsonStr);
		return null;
	}
	
	public String flush() throws BasicException {
		return "flush";
	}
	
	public MenuService getMenuService() {
		return menuService;
	}
	public void setMenuService(MenuService menuService) {
		this.menuService = menuService;
	}
	public MenuPO getMenu() {
		return menu;
	}
	public void setMenu(MenuPO menu) {
		this.menu = menu;
	}
	public FrameConstantService getFrameConstantService() {
		return frameConstantService;
	}
	public void setFrameConstantService(FrameConstantService frameConstantService) {
		this.frameConstantService = frameConstantService;
	}
	
}
